package com.lb.shopguide.ui.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.base.DialogBase;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.baselib.http.SimpleObserverOtherListener;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.order.CommitOrderReplyBean;
import com.lb.shopguide.event.guide.SwitchPayTypeEvent;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.shopguide.ui.activity.GuideMainActivity;
import com.lb.shopguide.util.json.JsonUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by kang on 2017/6/10.
 */

public class DialogChangePayType extends DialogBase implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private ImageButton ibClose;
    private TextView tvSure;
    private RelativeLayout layoutAliPay;
    private RelativeLayout layoutWechatPay;
    private RelativeLayout layoutCashPay;
    private RelativeLayout layoutBalancePay;

    private CheckBox chkAliPay;
    private CheckBox chkWechatPay;
    private CheckBox chkCashPay;
    private CheckBox chkBalancePay;

    private int checkPos;
    private CommitOrderReplyBean mReplyBean;
    private String mMemberCode;

    public static DialogChangePayType getDialogChangePayType(CommitOrderReplyBean replyBean, String memberCode) {
        DialogChangePayType dialogChangePayType = new DialogChangePayType();
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConstant.COMMIT_REPLY_BEAN, replyBean);
        bundle.putString(AppConstant.MEMBER_CODE, memberCode);
        dialogChangePayType.setArguments(bundle);
        return dialogChangePayType;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mFragmentView = inflater.inflate(R.layout.dialog_change_pay_type, container, false);
        initData();
        initView(mFragmentView);

        initListener();
        setDialogPosition();
        return mFragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.dialog_bottom);
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.COMMIT_REPLY_BEAN)) {
            mReplyBean = (CommitOrderReplyBean) bundle.getSerializable(AppConstant.COMMIT_REPLY_BEAN);
        }
        if (bundle.containsKey(AppConstant.MEMBER_CODE)) {
            mMemberCode = bundle.getString(AppConstant.MEMBER_CODE);
        }
    }

    private void initView(View view) {
        ibClose = view.findViewById(R.id.ib_close);
        tvSure = view.findViewById(R.id.tv_ok);
        layoutAliPay = view.findViewById(R.id.rl_ali_pay);
        layoutBalancePay = view.findViewById(R.id.rl_balance_pay);
        layoutCashPay = view.findViewById(R.id.rl_cash_pay);
        layoutWechatPay = view.findViewById(R.id.rl_wechat_pay);

        chkAliPay = view.findViewById(R.id.chk_ali_pay);
        chkBalancePay = view.findViewById(R.id.chk_balance_pay);
        chkCashPay = view.findViewById(R.id.chk_cash_pay);
        chkWechatPay = view.findViewById(R.id.chk_wechat_pay);
        bindData();
    }

    private void initListener() {
        ibClose.setOnClickListener(this);
        tvSure.setOnClickListener(this);

        layoutAliPay.setOnClickListener(this);
        layoutWechatPay.setOnClickListener(this);
        layoutCashPay.setOnClickListener(this);
        layoutBalancePay.setOnClickListener(this);

        chkAliPay.setOnCheckedChangeListener(this);
        chkWechatPay.setOnCheckedChangeListener(this);
        chkBalancePay.setOnCheckedChangeListener(this);
        chkCashPay.setOnCheckedChangeListener(this);
    }

    private void bindData() {
        if (Integer.parseInt(mReplyBean.getPayMode()) == AppConstant.PAY_TYPE_ALIPAY) {
            chkAliPay.setChecked(true);
        } else if (Integer.parseInt(mReplyBean.getPayMode()) == AppConstant.PAY_TYPE_WECHAT_PAY) {
            chkWechatPay.setChecked(true);
        } else if (Integer.parseInt(mReplyBean.getPayMode()) == AppConstant.PAY_TYPE_CASH_PAY) {
            chkCashPay.setChecked(true);
        } else if (Integer.parseInt(mReplyBean.getPayMode()) == AppConstant.PAY_TYPE_BALANCE_PAY) {
            chkBalancePay.setChecked(true);
        }
        if (TextUtils.isEmpty(mMemberCode)) {
            layoutBalancePay.setEnabled(false);
            chkBalancePay.setEnabled(false);
            chkBalancePay.setClickable(false);
            layoutBalancePay.setBackgroundColor(getResources().getColor(R.color.sg_common_line_bg_dark));
        } else {
            layoutBalancePay.setEnabled(true);
            chkBalancePay.setEnabled(true);
            chkBalancePay.setClickable(true);
            layoutBalancePay.setBackgroundColor(getResources().getColor(R.color.white));
        }
    }

    private void setDialogPosition() {
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL);
    }

    private void sendRequestChangePayOrder() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    CommitOrderReplyBean newBean = (CommitOrderReplyBean) JsonUtil.getObject(response.getReturnContent(), CommitOrderReplyBean.class);
                    mReplyBean.setOrderNum(newBean.getOrderNum());
                    mReplyBean.setOrderTotalMoney(newBean.getOrderTotalMoney());
                    mReplyBean.setPayQrCode(newBean.getPayQrCode());
                    mReplyBean.setPayMode("" + checkPos);
                    changePayOrderSuccess();
                }
            }
        };
        SimpleObserverOtherListener errorListener = new SimpleObserverOtherListener() {
            @Override
            public void onSessionOutOfDate() {
                ((GuideMainActivity) getActivity()).onSessionOutOfDate();
            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
            }
        };
        ApiMethodGuide.changeOrderPayMode(new SimpleLbObserver(listener, errorListener), mReplyBean.getOrderNum(), checkPos,this, FragmentEvent.DESTROY_VIEW);
    }

    private void changePayOrderSuccess() {
        EventBus.getDefault().post(new SwitchPayTypeEvent(mReplyBean));
        DialogChangePayType.this.dismiss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_close:
                DialogChangePayType.this.dismiss();
                break;
            case R.id.tv_ok:
                sendRequestChangePayOrder();
//                EventBus.getDefault().post(new SwitchPayTypeEvent(checkPos));
//                DialogChangePayType.this.dismiss();
                break;
            case R.id.rl_ali_pay:
                if (chkAliPay.isChecked()) {
                    chkAliPay.setChecked(false);
                } else {
                    clearCheck();
                    chkAliPay.setChecked(true);
                    checkPos = AppConstant.PAY_TYPE_ALIPAY;
                }
                break;
            case R.id.rl_wechat_pay:
                if (chkWechatPay.isChecked()) {
                    chkWechatPay.setChecked(false);
                } else {
                    clearCheck();
                    chkWechatPay.setChecked(true);
                    checkPos = AppConstant.PAY_TYPE_WECHAT_PAY;
                }
                break;
            case R.id.rl_cash_pay:
                if (chkCashPay.isChecked()) {
                    chkCashPay.setChecked(false);
                } else {
                    clearCheck();
                    chkCashPay.setChecked(true);
                    checkPos = AppConstant.PAY_TYPE_CASH_PAY;
                }
                break;
            case R.id.rl_balance_pay:
                if (chkBalancePay.isChecked()) {
                    chkBalancePay.setChecked(false);
                } else {
                    clearCheck();
                    chkBalancePay.setChecked(true);
                    checkPos = AppConstant.PAY_TYPE_BALANCE_PAY;
                }
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        clearCheck();
        if (isChecked) {
            buttonView.setChecked(isChecked);
            if (buttonView.getId() == R.id.chk_ali_pay) {
                checkPos = AppConstant.PAY_TYPE_ALIPAY;
            } else if (buttonView.getId() == R.id.chk_wechat_pay) {
                checkPos = AppConstant.PAY_TYPE_WECHAT_PAY;
            } else if (buttonView.getId() == R.id.chk_cash_pay) {
                checkPos = AppConstant.PAY_TYPE_CASH_PAY;
            } else if (buttonView.getId() == R.id.chk_balance_pay) {
                checkPos = AppConstant.PAY_TYPE_BALANCE_PAY;
            }
        }
    }

    private void clearCheck() {
        chkAliPay.setChecked(false);
        chkWechatPay.setChecked(false);
        chkCashPay.setChecked(false);
        chkBalancePay.setChecked(false);
        checkPos = -1;
    }
}
